package com.project;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@WebServlet("/UpdateUserServlet")
public class UpdateUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求和响应的字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 获取表单数据
        String field = request.getParameter("field");
        String newValue = request.getParameter("newValue");
        User user = (User) request.getSession().getAttribute("user");

        if (user == null) {
            response.getWriter().println("<html><body><h2>用户未登录，请先登录。</h2></body></html>");
            return;
        }

        // 准备输入参数
        List<String> input = new ArrayList<>();
        input.add(user.getName());
        input.add(user.getPid());
        input.add(user.getEmail());
        input.add(user.getPhone());
        input.add(user.getIdentity() + "");
        input.add(user.getIs_register() + "");
        input.add(user.getPassword());

        // 根据用户要修改的字段更新信息
        switch (field) {
            case "username":
                input.set(0, newValue);
                break;
            case "email":
                input.set(2, newValue);
                break;
            case "phone":
                input.set(3, newValue);
                break;
            case "password":
                input.set(6, newValue);
                break;
            default:
                response.getWriter().println("<html><body><h2>不支持修改该字段，请重试。</h2></body></html>");
                return;
        }

        // 调用 Information_Modification 方法
        boolean result = user.Information_Modification(input);

        if (result) {
            // 将修改请求存入ServletContext
            List<Map<String, String>> pendingRequests = (List<Map<String, String>>) getServletContext().getAttribute("pendingRequests");
            if (pendingRequests == null) {
                pendingRequests = new ArrayList<>();
            }
            
            Map<String, String> requestData = new HashMap<>();
            requestData.put("type", "modification");
            requestData.put("uid", user.getUid() + "");
            requestData.put("name", user.getName());
            requestData.put("pid", input.get(1));
            requestData.put("email", user.getEmail());
            requestData.put("phone", user.getPhone());
            requestData.put("identity", input.get(4));
            requestData.put("is_registered", input.get(5));
            requestData.put("password", user.getPassword());
            requestData.put("field", field);
            requestData.put("newValue", newValue);
            pendingRequests.add(requestData);
            
            getServletContext().setAttribute("pendingRequests", pendingRequests);
            response.sendRedirect("userInfo.jsp?pending=true");
        } else {
            // 修改失败，返回错误信息
            response.getWriter().println("<html><body><h2>修改用户信息失败，请重试。" + field + ", " + newValue + ", " + input + "</h2></body></html>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 通常将 GET 请求转发到 POST 处理方法
        doPost(request, response);
    }
}